package com.zy.mc_module.utils;

import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.os.Build;
import android.os.PersistableBundle;
import android.util.Log;

import static android.content.Context.JOB_SCHEDULER_SERVICE;

public class JobSchedulerUtils {
    public static void scheduleNotifications(Context context, Class<?> cls, int jobId, String title, String content, long time) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            try {
                JobScheduler jobScheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
                PersistableBundle bundle = new PersistableBundle();
                bundle.putString("title", title);
                bundle.putString("content", content);
                JobInfo jobInfo = new JobInfo.Builder(jobId, new ComponentName(context, cls))
                        .setRequiresCharging(false)
                        .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY) //任何有网络的状态
                        .setMinimumLatency(time)
                        .setOverrideDeadline(time + 2000L)
                        .setExtras(bundle) //系统重启后保留job
                        .setPersisted(false) //这里的单位是毫秒，1000 * 60 * 60 * 24代表一天（24小时）
                        .build();
                jobScheduler.schedule(jobInfo);
            } catch (Exception ex) {
                Log.e("czy", "scheduleNotifications failure");
            }
        }
    }

    public static void stopSchedule(Context context, int jobId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            JobScheduler jobScheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
            jobScheduler.cancel(jobId);
        }
    }
}
